package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo7WindowAll {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))

    /**
     * windowAll: 将同一个窗口内所有的数据放一起做计算
     */
    val windowAllDS: AllWindowedStream[(String, Int), TimeWindow] = kvDS
      .windowAll(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))

    val countDS: DataStream[(String, Int)] = windowAllDS.sum(1)

    countDS.print()

    env.execute()
  }

}
